package value

import (
	"context"
	"fmt"
	"sort"

	"tongquetai_server/activity_vk/domain/manage/common/static_data"
)

// @META.table:json
/*
[
    ["活跃战令配置"],
    ["免费/金币档位", 1],
    ["付费购买项", "battlepass_zntxz_499"],
    ["高级付费购买项", "battlepass_zntxz_999"],
    ["档位", "活跃积分", "免费/金币版奖励", "付费版奖励", "高级付费版奖励", "高级付费版奖励"],
    [1, 100, "金币*1", "金币*1", "金币*1", "金币*1"],
    [2, 200, "金币*1", "金币*1", "金币*1", "金币*1"],
    [3, 300, "金币*1", "金币*1", "金币*1", "金币*1"]
]
*/
// @META.struct:tqt
type TqtPassAwardItem struct {
	Level     int        `json:"level" header:"档位" validate:"gt=0"`
	Progress  int        `json:"progress" header:"活跃积分" validate:"gt=0"`
	FreeAward []TqtAward `json:"freeAward" allowEmpty:"true" header:"免费/金币版奖励" validate:"gte=0,dive"`
	PaidAward []TqtAward `json:"paidAward" header:"付费版奖励" validate:"gt=0,dive"`
	AdvAward  []TqtAward `json:"advAward" allowEmpty:"true" header:"高级付费版奖励" validate:"gte=0,dive"`
}
// @META.struct:tqt
type TqtPassAward struct {
	Coin         int                `json:"coin" splitter:"titleSplitter=活跃战令配置,minRowNum=5" parser:"commonSingleParser=1,1,免费/金币档位,true" exporter:"commonSingleExporter=免费/金币档位" validate:"gte=0" `
	PayItemId    string             `json:"payItemId" splitter:"titleSplitter=活跃战令配置,minRowNum=5" parser:"commonSingleParser=2,1,付费购买项,true" exporter:"commonSingleExporter=付费购买项"`
	AdvPayItemId string             `json:"advPayItemId" splitter:"titleSplitter=活跃战令配置,minRowNum=5" parser:"commonSingleParser=3,1,高级付费购买项,true" exporter:"commonSingleExporter=高级付费购买项"`
	Award        []TqtPassAwardItem `json:"award"  splitter:"titleSplitter=活跃战令配置,minRowNum=5" parser:"commonSliceParser=5" exporter:"commonSliceExporter" allowEmpty:"true" validate:"gte=0,dive"`
}

func (t *TqtPassAward) Call(ctx context.Context) (err error) {
	actType := ctx.Value("actType").(int)
	passAwardPayItemIdMap, err := static_data.GetPassAwardPayItemId().GetPassAwardPayItemIdMap(ctx, actType)
	if err != nil {
		return
	}
	// 校验购买项
	if len(t.PayItemId) != 0 {
		_, ok := passAwardPayItemIdMap[t.PayItemId]
		if !ok {
			return fmt.Errorf("活跃战令配置,付费购买项[%s]不存在", t.PayItemId)
		}
	}
	if t.AdvPayItemId != "" {
		_, ok := passAwardPayItemIdMap[t.AdvPayItemId]
		if !ok {
			return fmt.Errorf("活跃战令配置,高级付费购买项[%s]不存在", t.PayItemId)
		}
	}
	return nil
}

// @META.struct:svr
type SvrPassAward struct {
	Item  []SvrPassItem       `json:"item"`
	Level []SvrPassAwardLevel `json:"level"`
}
// @META.struct:svr
type SvrPassItem struct {
	Typ   int                `json:"typ"`
	Price SvrPrice           `json:"price"`
	Award []SvrPassAwardItem `json:"award"`
}
// @META.struct:svr
type SvrPrice struct {
	Price     SvrAward `json:"price"`
	PayItemId string   `json:"pay_item_id"`
}
// @META.struct:svr
type SvrPassAwardItem struct {
	Level int        `json:"level"`
	Award []SvrAward `json:"award"`
}
// @META.struct:svr
type SvrPassAwardLevel struct {
	Level    int `json:"level"`
	Progress int `json:"progress"`
}

/*
铜雀台 转 服务端

修改数据和格式符合服务端需求
1：数据必须从level=0开始，且level=0不能包含奖励数据
2：每一级的exp值表示到达下一级的经验差值，而excel导入的是算总值，需要重新计算
3：最后一级的exp必须等于0
*/

func TqtToSvrPassAward(tqtAward TqtPassAward) (res SvrPassAward) {
	res.Level = make([]SvrPassAwardLevel, len(tqtAward.Award))
	freeAward := make([]SvrPassAwardItem, 0)
	paidAward := make([]SvrPassAwardItem, 0)
	advAward := make([]SvrPassAwardItem, 0)
	for index, award := range tqtAward.Award {
		res.Level[index] = SvrPassAwardLevel{
			Level:    award.Level,
			Progress: award.Progress,
		}
		freeAward = append(freeAward, SvrPassAwardItem{
			Level: award.Level,
			Award: TqtToSvrAwards(award.FreeAward),
		})
		paidAward = append(paidAward, SvrPassAwardItem{
			Level: award.Level,
			Award: TqtToSvrAwards(award.PaidAward),
		})
		advAward = append(advAward, SvrPassAwardItem{
			Level: award.Level,
			Award: TqtToSvrAwards(award.AdvAward),
		})
	}
	res.Item = append(res.Item, SvrPassItem{
		Typ:   1,
		Award: freeAward,
	})
	if tqtAward.Coin != 0 {
		res.Item[0].Price = SvrPrice{
			Price: SvrAward{
				SvrItem: SvrItem{GameObjectId: "virtual_currency_1"},
				Count:   tqtAward.Coin,
			},
		}
	}
	res.Item = append(res.Item, SvrPassItem{
		Typ: 2,
		Price: SvrPrice{
			PayItemId: tqtAward.PayItemId,
		},
		Award: paidAward,
	})

	// 无 高级付费购买项 时, 整项配置都不给服务端
	if tqtAward.AdvPayItemId != "" {
		res.Item = append(res.Item, SvrPassItem{
			Typ: 3,
			Price: SvrPrice{
				PayItemId: tqtAward.AdvPayItemId,
			},
			Award: advAward,
		})
	}

	return
}

/*
服务端 转 铜雀台
*/

func SvrToTqtPassAward(ctx context.Context, svrAward SvrPassAward) (res TqtPassAward) {
	res.Award = make([]TqtPassAwardItem, 0)
	progressMap := make(map[int]TqtPassAwardItem)
	freeAwardMap := make(map[int][]TqtAward)
	paidAwardMap := make(map[int][]TqtAward)
	advAwardMap := make(map[int][]TqtAward)
	for _, v := range svrAward.Level {
		progressMap[v.Level] = TqtPassAwardItem{
			Level:    v.Level,
			Progress: v.Progress,
		}
	}

	for _, item := range svrAward.Item {
		switch item.Typ {
		case 1:
			res.Coin = item.Price.Price.Count
		case 2:
			res.PayItemId = item.Price.PayItemId
		case 3:
			res.AdvPayItemId = item.Price.PayItemId
		}
		for _, award := range item.Award {
			if _, ok := progressMap[award.Level]; ok {
				switch item.Typ {
				case 1:
					freeAwardMap[award.Level] = SvrToTqtAwards(ctx, award.Award)
				case 2:
					paidAwardMap[award.Level] = SvrToTqtAwards(ctx, award.Award)
				case 3:
					advAwardMap[award.Level] = SvrToTqtAwards(ctx, award.Award)
				}
			}
		}
	}
	for _, award := range progressMap {
		awardItem := TqtPassAwardItem{
			Level:    award.Level,
			Progress: award.Progress,
		}
		if award, ok := freeAwardMap[award.Level]; ok {
			awardItem.FreeAward = award
		}
		if award, ok := paidAwardMap[award.Level]; ok {
			awardItem.PaidAward = award
		}
		if award, ok := advAwardMap[award.Level]; ok {
			awardItem.AdvAward = award
		}
		res.Award = append(res.Award, awardItem)
	}
	sort.SliceStable(res.Award, func(a, b int) bool {
		return res.Award[a].Level < res.Award[b].Level
	})
	return
}
